/**
 * 定义监测仪器管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.WasteWater.dataTransValidation.powerOutageManager', ['water.entity.powerOutage','water.common.dateUtil']);
	
	module.controller('powerOutageManager',function($rootScope,$scope,$powerOutageService,$controller,$monitorStationService,$dateUtil,$state){     
		$scope.selectedModel={};
		/**
		 * 查询条件参数集
		 */
		$scope.queryObject={};
		/**
		 * 查询条件参数集
		 */
		$scope.queryParams={};
		/**
		 * 新增按钮点击事件
		 */
		$scope.onCreateClick = function(){
			//根据STATION_ID查询相应的MN号及所属企业名称
			var queryObject = {
					"STATION_ID":$scope.selectedModel.STATION_ID,
				};
			$powerOutageService.getBaseInfoByStationID(queryObject).then(function(data){
				$scope.powerOutageCreate = {};
				$scope.powerOutageCreate.COMPANYNAME = data.data[0].COMPANYNAME;
				$scope.powerOutageCreate.POINTNAME  = data.data[0].POINTNAME;
				$scope.powerOutageCreate.MN = data.data[0].MN;
				$scope.createDialog.open();
			});	
		}
		
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(curRow){
			$scope.powerOutageEdit= angular.copy(curRow);
			$scope.editDialog.open();
		}
		
		$scope.$on('dataPramChanged', function(event, station) {
			$scope.selectedModel = station;
			$scope.getPointName(0);
	    });
		
		//站点选中事件
		$scope.getPointName=function(orgid){
			var li3 = $("#pointName li[orgid='"+-1+"']");
			var li = $("#pointName li[orgid='"+0+"']");
			if(orgid=="")
			{
				var li1=$("#pointName li");
				li1.removeClass('selected');
				li3.addClass('selected');

				return;
			}
			if(orgid=="-1")
			{
				li3.addClass('selected');
				li.removeClass('selected');
				$scope.queryObject.MN="0";
			}
			else{
				li3.removeClass('selected');
				li.addClass('selected');
				$scope.queryObject.MN=$scope.selectedModel.MN;
			}
		};				
		
		//时间验证
		$scope.checkErr = function(){		
			if(typeof $scope.queryObject.start!="undefined")
			{
			    if((new Date($scope.queryObject.start.replace(/-/g,"\/"))) > (new Date($scope.queryObject.end.replace(/-/g,"\/")))){
			    	$.messager.alert("提示","开始时间不能大于结束时间");
			      return false;
			    }	
			}
		 };		
		
		$scope.queryPowerOutage = function() {
			if($scope.queryObject.MN!="0")
			{
				$scope.queryObject.MN=$scope.selectedModel.MN;
			}
			$scope.powerOutageList="";
			$scope.queryObject.END_TIME =$scope.queryParams.end+ " 23:59:59";
			$scope.queryObject.START_TIME = $scope.queryParams.start+ " 00:00:00";
				
			$waterDataCommon.getPageRows("/IndusteryPollutQuarterDataValidation/queryPowerOutage.do","powerOutageList",$scope.queryObject,100,1,function(d){
					$scope.powerOutageList=d.data;
			});
		};
		
		/**
		 * 新增停电时间段
		 */
		$scope.onCreatePowerOutage = function(curData){
			$saveObj={};
			$saveObj.STATION_ID = $scope.selectedModel.MN;
			$saveObj.STATION_NAME = curData.POINTNAME;
			$saveObj.COMPANY_NAME = curData.COMPANYNAME;
			var start = curData.NO_POWER_START_TIME;
			var end = curData.NO_POWER_END_TIME;
			$saveObj.MARK = curData.MARK;
			
			if(start==null) {
				$.messager.alert("时间出错","时间出错",'info');
				return;
			} else {
				var flag = /\//g;
				var st_index=0;
				st_index = start.indexOf("/");
				if(st_index>0) {
					start = start.replace(flag,"-");
					start=start+":00";
				}
			}
			if(end==null) {
				$.messager.alert("时间出错","时间出错",'info');
				return;
			} else {
				var flag = /\//g;
				var end_index=0;
				end_index = end.indexOf("/");
				if(end_index>0) {
					end = end.replace(flag,"-");
					end=end+":00";
				}
			}
			$saveObj.NO_POWER_START_TIME = start;
			$saveObj.NO_POWER_END_TIME = end;
			
			var start_time="";
			var end_time="";
			var flag = /\//g;
			var startArray=[];
			var endArray=[];
			//查询停电时间
			$scope.noPowerList="";
			 $scope.queryObjectC={};
			 $scope.queryObjectC.STATION_ID=$saveObj.STATION_ID;

			 $waterDataCommon.getPageRows("/IndusteryPollutQuarterDataValidation/queryNoPowerOnlyByStationId.do","noPowerList",$scope.queryObjectC,100,1,function(d){
					$scope.noPowerList=d.data;
			
			
			if($scope.noPowerList.length>0) {
				for(var k =0;k<$scope.noPowerList.length;k++) {
					var st_index=0;
					var end_index=0;
					if($scope.noPowerList[k].NO_POWER_START_TIME==null ||$scope.noPowerList[k].isNew==true) {
						continue;
					}
					if($scope.noPowerList[k].NO_POWER_END_TIME==null) {
						continue;
					}
					st_index = $scope.noPowerList[k].NO_POWER_START_TIME.indexOf("/");
					end_index = $scope.noPowerList[k].NO_POWER_END_TIME.indexOf("/");
					if(st_index>0) {
						$scope.noPowerList[k].NO_POWER_START_TIME = $scope.noPowerList[k].NO_POWER_START_TIME.replace(flag,"-");
						$scope.noPowerList[k].NO_POWER_START_TIME=$scope.noPowerList[k].NO_POWER_START_TIME+":00";
					}
					if(end_index>0) {
						$scope.noPowerList[k].NO_POWER_END_TIME = $scope.noPowerList[k].NO_POWER_END_TIME.replace(flag,"-");
						$scope.noPowerList[k].NO_POWER_END_TIME=$scope.noPowerList[k].NO_POWER_END_TIME+":00";
					}
					start_time += $scope.noPowerList[k].NO_POWER_START_TIME+",";
					startArray.push($scope.noPowerList[k].NO_POWER_START_TIME);
					end_time += $scope.noPowerList[k].NO_POWER_END_TIME+",";
					endArray.push($scope.noPowerList[k].NO_POWER_END_TIME);
				}
			}
			//校验是否为空
			var flag = true;
			flag = checkNull($saveObj);
			
			if(flag==false) {
				return;
			}
			//校验新增的日期是否存在重叠
			flag = checkTimeOverLap(startArray,endArray,$saveObj);
			if(flag==false) {
				return;
			}
			//校验新增的日期是否存在重复
			flag = checkTimeDouble($saveObj,$scope.noPowerList);
			if(flag==false) {
				return;
			}
			$waterDataCommon.createModel("BC_SNP_INFO",$saveObj).then(function(){
				$scope.queryPowerOutage();
				$.messager.alert("保存成功","保存成功",'info');
				$scope.createDialog.close();
			});
			 });
			
			
			
		};
		
		/**
		 * 更新停电时间段
		 */
		$scope.onEditPowerOutage = function(curData){
			$saveObj={};
			$saveObj.MN = curData.STATION_ID;
			$saveObj.STATION_NAME = curData.STATION_NAME;
			var start = curData.NO_POWER_START_TIME;
			var end = curData.NO_POWER_END_TIME;
			$saveObj.MARK = curData.MARK;
			$saveObj.GUID = curData.GUID;

			if(start==null) {
				$.messager.alert("时间出错","时间出错",'info');
				return;
			} else {
				var flag = /\//g;
				var st_index=0;
				st_index = start.indexOf("/");
				if(st_index>0) {
					start = start.replace(flag,"-");
					start=start+":00";
				}
			}
			if(end==null) {
				$.messager.alert("时间出错","时间出错",'info');
				return;
			} else {
				var flag = /\//g;
				var end_index=0;
				end_index = end.indexOf("/");
				if(end_index>0) {
					end = end.replace(flag,"-");
					end=end+":00";
				}
			}
			$saveObj.NO_POWER_START_TIME = start;
			$saveObj.NO_POWER_END_TIME = end;
			
			var start_time="";
			var end_time="";
			var flag = /\//g;
			var startArray=[];
			var endArray=[];
			//查询停电时间
			$scope.noPowerList="";
			$scope.queryObjectE={};
			$scope.queryObjectE.STATION_ID=$saveObj.MN;
			$scope.queryObjectE.FLAG = "edit";
			$scope.queryObjectE.GUID = curData.GUID;

			$waterDataCommon.getPageRows("/IndusteryPollutQuarterDataValidation/queryNoPowerOnlyByStationId.do","noPowerList",$scope.queryObjectE,100,1,function(d){
			$scope.noPowerList=d.data;
			if($scope.noPowerList.length>0) {
				for(var k =0;k<$scope.noPowerList.length;k++) {
					var st_index=0;
					var end_index=0;
					if($scope.noPowerList[k].NO_POWER_START_TIME==null ||$scope.noPowerList[k].isNew==true) {
						continue;
					}
					if($scope.noPowerList[k].NO_POWER_END_TIME==null) {
						continue;
					}
					st_index = $scope.noPowerList[k].NO_POWER_START_TIME.indexOf("/");
					end_index = $scope.noPowerList[k].NO_POWER_END_TIME.indexOf("/");
					if(st_index>0) {
						$scope.noPowerList[k].NO_POWER_START_TIME = $scope.noPowerList[k].NO_POWER_START_TIME.replace(flag,"-");
						$scope.noPowerList[k].NO_POWER_START_TIME=$scope.noPowerList[k].NO_POWER_START_TIME+":00";
					}
					if(end_index>0) {
						$scope.noPowerList[k].NO_POWER_END_TIME = $scope.noPowerList[k].NO_POWER_END_TIME.replace(flag,"-");
						$scope.noPowerList[k].NO_POWER_END_TIME=$scope.noPowerList[k].NO_POWER_END_TIME+":00";
					}
					start_time += $scope.noPowerList[k].NO_POWER_START_TIME+",";
					startArray.push($scope.noPowerList[k].NO_POWER_START_TIME);
					end_time += $scope.noPowerList[k].NO_POWER_END_TIME+",";
					endArray.push($scope.noPowerList[k].NO_POWER_END_TIME);
				}
			}
			//校验是否为空
			var flag = true;
			flag = checkNull($saveObj);
			
			if(flag==false) {
				return;
			}
			//校验新增的日期是否存在重叠
			flag = checkTimeOverLap(startArray,endArray,$saveObj);
			if(flag==false) {
				return;
			}
			//校验新增的日期是否存在重复
			flag = checkTimeDouble($saveObj,$scope.noPowerList);
			if(flag==false) {
				return;
			}
			$waterDataCommon.getPageRows("/IndusteryPollutQuarterDataValidation/updatePowerOutage.do","hd",$saveObj,function(d) {
				$.messager.alert("保存成功","保存成功",'info');
				$scope.editDialog.close();
			});
			$scope.queryPowerOutage();
			 });
			

		};
		
		/**
		 * 删除停电时间段
		 */
		$scope.deletePowerOutage = function(curData){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$waterDataCommon.getPageRows("/IndusteryPollutQuarterDataValidation/deleteNoPowerTime.do","hd",curData,function(d) {	
					});
					$scope.queryPowerOutage();
				}
			});
		};
		
		//验证时间是否存在重叠
		function checkTimeOverLap(startArray,endArray,curData) {
		    if(startArray==null || startArray.length<=1) {
		    	return true;
		    }
			var startTimeArr = [];
		    var endTimeArr = [];
		    startTimeArr=startArray;
		    endTimeArr = endArray;
		    var timeE = '',timeS = '';
		    var startTimeArr = startTimeArr.sort();
		    var endTimeArr = endTimeArr.sort();
		    for(var k=0;k<startTimeArr.length;k++){
		        if (curData.NO_POWER_END_TIME<=startTimeArr[k]||curData.NO_POWER_START_TIME>=endTimeArr[k]){
		            continue;
		        }
		        else
		        {
		        	$.messager.alert("提示","时间段存在重叠");
		            return false;
		        }
		    }
		    return true;
		};
		//验证时间是否存在重复
		function checkTimeDouble(newTime,oldTimeArray) {
			var startTime ="";
		    var endTime = "";
		    if(newTime==null ||newTime.length==0) {
		    	return true;
		    }
		    startTime = newTime.NO_POWER_START_TIME;
		    endTime = newTime.NO_POWER_END_TIME;
		    if(oldTimeArray==null || oldTimeArray.length<=1) {
		    	return true;
		    }
		    for(var k=0;k<oldTimeArray.length;k++){
		    	var start1 = oldTimeArray[k].NO_POWER_START_TIME;
		    	var end1 = oldTimeArray[k].NO_POWER_END_TIME;
		    	if(start1==startTime && end1==endTime) {
		            $.messager.alert("提示","时间段重复");
		            return false;
		    	}
		    }
		    return true;
		};
		/**
		 * 检查新增停电时间段是否为空
		 */
		function checkNull(curData) {
			if(curData.NO_POWER_START_TIME==null||curData.NO_POWER_START_TIME=='') {
				$.messager.alert("提示","停电时段开始时间不能为空");
				return false;
			}
			if(curData.NO_POWER_END_TIME==null||curData.NO_POWER_END_TIME=='') {
				$.messager.alert("提示","停电时段结束时间不能为空");
				return false;
			}
			if(curData.NO_POWER_END_TIME<curData.NO_POWER_START_TIME) {
				$.messager.alert("提示","开始时间不能大于结束时间");
				return false;
			}
			
			return true;
		};
		
		$scope.queryNoPowerTimeByStationID = function(STATION_ID) {
			 $scope.noPowerList="";
			 $scope.queryObject.STATION_ID=STATION_ID;

			 $waterDataCommon.getPageRows("/IndusteryPollutQuarterDataValidation/queryNoPowerOnlyByStationId.do","noPowerList",$scope.queryObject,100,1,function(d){
					$scope.noPowerList=d.data;
			});
		};
		/**
		 * 初始化
		 */
		function _init(){

		}
		
		_init();
	});
}(window, angular));	